﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using LCUV;

namespace sWarranty.WEB
{
    public partial class SOMSerialMgrByD : System.Web.UI.Page
    {
        public string _currentUserEmpNo = "";
        public string _currentUserAuthority = "";
        public string _currentUserRegion = "";

        //page nay chua xet theo curUserRegion

        protected void Page_Load(object sender, EventArgs e)
        {
            //btnFind.Focus();
            _currentUserEmpNo = GetUserLoginEmpNo();
            Session["UrltoFw"] = "SOMSerialMgrByD.aspx";

            if (_currentUserEmpNo == "")
                Response.Redirect("login.aspx");
            if (!IsPostBack)
            {
                LoadResponseType();
                txtToDate.Text = DateTime.Now.ToString("MM/dd/yyyy");
                int dateTOShow = 0 - Convert.ToInt32(DateTime.Now.DayOfWeek) + 1; //hom nay thu dateTOShow, thi chi hien den cot D dateTOShow
                txtFromDate.Text = DateTime.Now.AddDays(dateTOShow).ToString("MM/dd/yyyy");
            }

        }
        protected string GetUserLoginEmpNo()
        {
            string t = "";
            try
            {
                if (Session["UserLoginInfo"] != null)
                {
                    Entities db = new Entities();
                    var u = (TB_USER[])Session["UserLoginInfo"];
                    int _UID = Convert.ToInt32(u.Single().ID);
                    var au = from ft in db.TB_USER_LEVEL
                             where ft.USER_ID == _UID
                             select ft;
                    int _le = Convert.ToInt32(au.Single().LEVEL_ID);
                    var le = from xx in db.TB_LEVEL
                             where xx.ID == _le
                             select xx;
                    _currentUserAuthority = le.Single().REMARK;
                    _currentUserRegion = u.Single().REGION_ID;
                    if (_currentUserRegion != "00")
                    {
                        DBAction.SetSelected(dropRegion, (u.Single().REGION_ID));
                        dropRegion.Enabled = false;
                    }
                    return u.Single().EMPNO;
                }
            }
            catch (Exception)
            {
                t = "";
            }
            return t;
        }
        protected void BuildTable(DateTime from, DateTime to, string p_hist_type, string curUserRegion, string region,
            string p_resp_type, string p_shop_cell, string p_mo_msg)
        {
            DataTable dt = new DataTable();
            dt = DBAction.runStore("PK_SOM_MST.PR_SO_DLY_HIS_INQ", "uiroi", "p_df_date", from.ToString("yyyyMMdd"), "p_dt_date", to.ToString("yyyyMMdd"),
                "p_hist_type", p_hist_type, "p_user_region", curUserRegion, "p_region_id", region,
                "p_resp_type", p_resp_type, "p_shop_cell", p_shop_cell, "p_mo_msg", p_mo_msg, null, null, null, null
                , null, null, null, null, null, null, null, null, null, null);
            try
            {
                if (dt.Rows.Count > 0 && !string.IsNullOrEmpty(dt.Rows[0]["Error"].ToString()))
                {
                    MessageBox.Show(dt.Rows[0]["Error"].ToString());
                    return;
                }
            }
            catch (Exception)
            {
                //Column 'Error' does not belong to table <-- ko co' loi j va co' data , thi ko lam j
            }
            RadGrid1.DataSource = dt;
            RadGrid1.DataBind();
        }
        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "click", "var btn=document.getElementById('btnFind');btn.click();", true);
            string region = "%";
            DateTime from = Convert.ToDateTime(txtFromDate.Text);
            DateTime to = Convert.ToDateTime(txtToDate.Text);
            string p_hist_type = dropSMSType.SelectedValue;
            string p_resp_type = "%";
            string p_shop_cell = null;
            string p_mo_msg = null;

            if (dropRegion.SelectedValue != "00")
                region = dropRegion.SelectedValue;
            p_resp_type = dropRspType.SelectedValue;
            if (!string.IsNullOrEmpty(txtShopCell.Text))
                p_shop_cell = txtShopCell.Text.Trim();
            if (!string.IsNullOrEmpty(txtMsgBody.Text))
                p_mo_msg = txtMsgBody.Text.Trim();

            DataTable dt = new DataTable();
            dt = DBAction.runStore("PK_SOM_MST.PR_SO_DLY_HIS_INQ", "uiroi", "p_df_date", from.ToString("yyyyMMdd"), "p_dt_date", to.ToString("yyyyMMdd"),
                "p_hist_type", p_hist_type, "p_user_region", _currentUserRegion, "p_region_id", region,
                "p_resp_type", p_resp_type, "p_shop_cell", p_shop_cell, "p_mo_msg", p_mo_msg, null, null, null, null
                , null, null, null, null, null, null, null, null, null, null);
            try
            {
                if (dt.Rows.Count > 0 && !string.IsNullOrEmpty(dt.Rows[0]["Error"].ToString()))
                {
                    MessageBox.Show(dt.Rows[0]["Error"].ToString());
                    return;
                }
            }
            catch (Exception)
            {
                //Column 'Error' does not belong to table <-- ko co' loi j va co' data , thi ko lam j
            }
            RadGrid1.DataSource = dt;
        }
        protected void btnFind_Click(object sender, EventArgs e)
        {
            string region = "%";
            DateTime from = Convert.ToDateTime(txtFromDate.Text);
            DateTime to = Convert.ToDateTime(txtToDate.Text);
            string p_hist_type = dropSMSType.SelectedValue;
            string p_resp_type = "%";
            string p_shop_cell = null;
            string p_mo_msg = null;

            if (dropRegion.SelectedValue != "00")
                region = dropRegion.SelectedValue;
            p_resp_type = dropRspType.SelectedValue;
            if (!string.IsNullOrEmpty(txtShopCell.Text))
                p_shop_cell = txtShopCell.Text.Trim();
            if (!string.IsNullOrEmpty(txtMsgBody.Text))
                p_mo_msg = txtMsgBody.Text.Trim();

            BuildTable(from, to, p_hist_type, _currentUserRegion, region, p_resp_type, p_shop_cell, p_mo_msg);
        }
        protected void RadGrid1_GroupsChanging(object sender, GridGroupsChangingEventArgs e)
        {
            if (e.Action == GridGroupsChangingAction.Group)
                RadGrid1.MasterTableView.GetColumnSafe(e.Expression.GroupByFields[0].FieldName).Visible = false;
            else
                if (e.Action == GridGroupsChangingAction.Ungroup)
                {
                    //if (_currentUserRegion != "00")
                    //    RadGrid1.MasterTableView.GetColumnSafe(e.Expression.GroupByFields[0].FieldName).Visible = false;
                    //else
                    RadGrid1.MasterTableView.GetColumnSafe(e.Expression.GroupByFields[0].FieldName).Visible = true;
                }
        }

        protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
        {

            if (e.Column is GridBoundColumn)
            {
                (e.Column as GridBoundColumn).DataFormatString = "<nobr>{0}</nobr>"; //auto width
                (e.Column as GridBoundColumn).HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
                (e.Column as GridBoundColumn).HeaderStyle.Font.Bold = true;    
            }
        }
        protected void LoadResponseType()
        {
            Entities db = new Entities();
            var re = from t in db.TB_MT_SMS_RESP_MSG
                     orderby t.RESP_TYPE
                     select t;
            dropRspType.DataSource = re;
            dropRspType.DataTextField = "PURPOSE";
            dropRspType.DataValueField = "RESP_TYPE";
            dropRspType.DataBind();

            ListItem tt = new ListItem();
            tt.Value = "%";
            tt.Text = "All";
            dropRspType.Items.Insert(0, tt);
        }

        protected void btnExcel_Click(object sender, EventArgs e)
        {
            RadGrid1.ExportSettings.IgnorePaging = true;
            RadGrid1.ExportSettings.OpenInNewWindow = true;
            RadGrid1.ExportSettings.FileName = "data";
            RadGrid1.MasterTableView.ExportToExcel();
        }

        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (_currentUserRegion != "00")
            {
                GridColumn column = RadGrid1.MasterTableView.GetColumnSafe("REGION");
                if (column != null)
                {
                    column.Visible = false;
                }
            }
            //if (e.Item is GridDataItem)
            //{
            //    GridDataItem item = (GridDataItem)e.Item;
            //    item["AMT"].Text = String.Format("{0:#,0}", item["AMT"].Text);
            //}
        }
    }
}